package cn.Y_Qiu.org.controller;

import cn.Y_Qiu.basic.exception.BusinessException;
import cn.Y_Qiu.basic.util.Ajaxresult;
import cn.Y_Qiu.org.domain.LoginDto;
import cn.Y_Qiu.org.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    @RequestMapping("/account")
    public Ajaxresult login(@RequestBody LoginDto loginDto){
        try {
            return loginService.login(loginDto);
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me().setMessage(e.getMessage());
        }
    }

    /**
     * 微信登录流程
     * @param params
     * @return
     */
    @PostMapping("/wechat")
    public Ajaxresult wechat(@RequestBody Map<String,String> params){
        try {
            return loginService.wechat(params);
        } catch (BusinessException e) {
            e.printStackTrace();
            return Ajaxresult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me().setMessage("系统繁忙，稍后重试！！");
        }

    }

    @PostMapping("/binder/wechat")
    public Ajaxresult binder(@RequestBody Map<String,String> params){
        try {
            return loginService.binder(params);
        } catch (BusinessException e) {
            e.printStackTrace();
            return Ajaxresult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me().setMessage("系统繁忙，稍后重试！！");
        }

    }

}
